package com.fin.zw.intelligentbot.controller;


import com.fin.zw.intelligentbot.entity.RoleInfo;
import com.fin.zw.intelligentbot.model.request.RoleInfoRequest;
import com.fin.zw.intelligentbot.service.RoleInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/role")
@Api(value = "角色相关管理",description = "角色相关管理")
public class RoleInfoController {

    @Autowired
    private RoleInfoService roleInfoService;

    @GetMapping("/fetchPage")
    @ApiOperation(value = "查询所有的角色信息",notes = "查询所有的角色信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", dataType = "int", paramType = "query",
                    value = "页数 (0..N)"),
            @ApiImplicitParam(name = "size", dataType = "int", paramType = "query",
                    value = "每页大小."),
            @ApiImplicitParam(name = "sort", allowMultiple = true, dataType = "string", paramType = "query",
                    value = "依据什么排序: 属性名(,asc|desc). ")
    })
    public ResponseEntity<Page<RoleInfo>> queryRoleInfoAll(Pageable pageable,
                                                           RoleInfoRequest request){
        Page<RoleInfo> page = roleInfoService.queryRoleInfoAll(pageable,request);
        return ResponseEntity.ok().body(page);
    }

    @GetMapping("/fetch")
    @ApiOperation(value = "查询所有的角色信息", notes = "查询所有的角色信息")
    public ResponseEntity<List<RoleInfo>> getRoleInfoList() {
        List<RoleInfo> page = roleInfoService.getRoleInfoList();
        return ResponseEntity.ok().body(page);
    }


    @PostMapping("/add")
    @ApiOperation(value = "创建角色",notes = "创建角色")
    public ResponseEntity<String> createRoleInfo(@RequestBody RoleInfoRequest request){
        roleInfoService.createRoleInfo(request);
        return ResponseEntity.ok().body("操作成功");
    }

    @RequestMapping(value = "/update",method = {RequestMethod.PUT,RequestMethod.PATCH})
    @ApiOperation(value = "角色修改",notes = "角色修改")
    public ResponseEntity<String> editRoleInfo(@RequestBody RoleInfoRequest request){
        roleInfoService.updateRoleInfo(request);
        return ResponseEntity.ok().body("操作成功");
    }


    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "角色信息删除",notes = "角色信息删除")
    public ResponseEntity<String> deleteRoleInfo(@PathVariable String id){
        roleInfoService.deleteRoleInfoById(id);
        return ResponseEntity.ok().body("删除成功");
    }
}
